package com.jcli.jb51_net.article._界面样式;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SwingMainUI01 extends JFrame {
    private JSplitPane mainPanel;
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JPanel tablePanel;
    private JPanel logPanel;
    private JButton btn1, btn2, btn3, btn4, btn5, btn6;
    private JLabel logLabel;

    private JTable table;

    public SwingMainUI01() {
        setTitle("Swing Main UI");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 创建菜单栏
        JMenuBar menuBar = new JMenuBar();

        // 创建菜单
        JMenu fileMenu = new JMenu("File");
        JMenu helpMenu = new JMenu("Help");

        // 创建菜单项
        JMenuItem exitItem = new JMenuItem("Exit");
        JMenuItem aboutItem = new JMenuItem("About");

        // 为菜单项添加点击事件监听器
        exitItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        aboutItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(SwingMainUI01.this, "Swing Main UI Example", "About", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        // 将菜单项添加到菜单
        fileMenu.add(exitItem);
        helpMenu.add(aboutItem);

        // 将菜单添加到菜单栏
        menuBar.add(fileMenu);
        menuBar.add(helpMenu);

        // 将菜单栏添加到窗口
        setJMenuBar(menuBar);

        // 创建主面板
        mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

        // 创建左边面板
        leftPanel = new JPanel(new GridLayout(6, 1));
        btn1 = new JButton("Button 1");
        btn2 = new JButton("Button 2");
        btn3 = new JButton("Button 3");
        btn4 = new JButton("Button 4");
        btn5 = new JButton("Button 5");
        btn6 = new JButton("Button 6");

        // 为按钮添加点击事件监听器
        // btn1.addActionListener(new ButtonClickListener());
        btn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // showTableAndLogPanel();
            }
        });
        btn2.addActionListener(new ButtonClickListener());
        btn3.addActionListener(new ButtonClickListener());
        btn4.addActionListener(new ButtonClickListener());
        btn5.addActionListener(new ButtonClickListener());
        btn6.addActionListener(new ButtonClickListener());

        // 将按钮添加到左边面板
        leftPanel.add(btn1);
        leftPanel.add(btn2);
        leftPanel.add(btn3);
        leftPanel.add(btn4);
        leftPanel.add(btn5);
        leftPanel.add(btn6);

        // 创建右边面板
        rightPanel = new JPanel(new CardLayout());

        // 创建上面板（表格）
        tablePanel = new JPanel();
        // 这里可以添加表格组件

        // 创建下面板（日志）
        logPanel = new JPanel(new BorderLayout());
        logLabel = new JLabel("Log Area");
        logPanel.add(logLabel, BorderLayout.CENTER);

        // 将上下两个面板添加到右边面板
        rightPanel.add(tablePanel, "TablePanel");
        rightPanel.add(logPanel, "LogPanel");

        // 将左右两个面板添加到主面板
        mainPanel.setLeftComponent(leftPanel);
        mainPanel.setRightComponent(rightPanel);

        // 设置左右两个面板的相对宽度比例
        int leftWidth = (int) (getWidth() * 0.3);
        int rightWidth = (int) (getWidth() * 0.7);
        mainPanel.setDividerLocation(leftWidth);

        // 设置默认显示的右边面板内容
        CardLayout cardLayout = (CardLayout) rightPanel.getLayout();
        cardLayout.show(rightPanel, "TablePanel");

        // 将主面板添加到窗口
        add(mainPanel);

        setVisible(true);
    }

    // 按钮点击事件监听器
    private class ButtonClickListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JButton source = (JButton) e.getSource();

            // 根据点击的按钮切换右边面板内容
            CardLayout cardLayout = (CardLayout) rightPanel.getLayout();
            if (source == btn1) {
                cardLayout.show(rightPanel, "TablePanel");
                logLabel.setText("1Log Area (TablePanel)");
            } else if (source == btn2) {
                cardLayout.show(rightPanel, "LogPanel");
                logLabel.setText("2Log Area (LogPanel)");
            }else if (source == btn3) {
                cardLayout.show(rightPanel, "LogPanel");
                logLabel.setText("3Log Area (LogPanel)");
            }else if (source == btn4) {
                cardLayout.show(rightPanel, "LogPanel");
                logLabel.setText("4Log Area (LogPanel)");
            }else if (source == btn5) {
                cardLayout.show(rightPanel, "LogPanel");
                logLabel.setText("5Log Area (LogPanel)");
            }else if (source == btn6) {
                cardLayout.show(rightPanel, "LogPanel");
                logLabel.setText("6Log Area (LogPanel)");
            } // 添加其他按钮的处理逻辑

            // 这里可以根据需要添加其他按钮的处理逻辑
        }


    }

   /* private void showTableAndLogPanel() {
        tablePanel = new JPanel();
        logPanel = new JPanel();

        // Create a sample table model
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("Column 1");
        model.addColumn("Column 2");
        model.addRow(new Object[]{"Data 1-1", "Data 1-2"});
        model.addRow(new Object[]{"Data 2-1", "Data 2-2"});

        table = new JTable(model);
        JScrollPane tableScrollPane = new JScrollPane(table);

        logLabel = new JLabel("Log Area");
        logPanel.add(logLabel);

        rightPanel.setTopComponent(tableScrollPane);
        rightPanel.setBottomComponent(logPanel);
    }*/


    public static void main(String[] args) {
        SwingUtilities.invokeLater(SwingMainUI01::new);
    }
}
